/*
* 一个三次贝塞尔曲线的采样类
* 节点位置分别为(0,0)(a,0)(1-b,1)(1,1)
*/
class easyEase_easyCubic {
    //预计算的样本
    samples_x = []
    samples_y = []

    constructor(a, b, times = 20) {
        this.preCalculate(a, b, times)
    }

    //times表示采样次数
    preCalculate(a, b, times = 20) {
        this.samples_x=[0]
        this.samples_y=[0]

        for(var i=1; i<times; i++) {
            var k = i/times
            //第一次迭代
            var x0 = a*k, x1 = a*(1-k)+(1-b)*k, x2 = 1-b+b*k
            var y0 = 0, y1 = k, y2 = 1
            //第二次迭代
            x0 = x0*(1-k) + x1*k
            x1 = x1*(1-k) + x2*k
            y0 = y0*(1-k) + y1*k
            y1 = y1*(1-k) + y2*k
            //第三次迭代
            x0 = x0*(1-k) + x1*k
            y0 = y0*(1-k) + y1*k

            //写入
            this.samples_x.push(x0)
            this.samples_y.push(y0)
        }

        this.samples_x.push(1)
        this.samples_y.push(1)
    }

    //采样 t取值0~1
    sample(t) {
        if (t <= 0 ) return 0
        if (t >= 1 ) return 1

        for (var i = 1; i < this.samples_x.length; i++) {
            if (this.samples_x[i] > t) {
                var k = (t-this.samples_x[i-1])/(this.samples_x[i]-this.samples_x[i-1])
                return this.samples_y[i]*k + this.samples_y[i-1]*(1-k)
            } 
        }
    }
}

const easyEase_easyCubic_0 = new easyEase_easyCubic(0.2, 0.2)
const easyEase_easyCubic_1 = new easyEase_easyCubic(0.4, 0.4)

module.exports = {
    easyEase_easyCubic,
    easyEase_easyCubic_0,
    easyEase_easyCubic_1
}